package com.ruoyi.web.controller.jjm;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjm.domain.AreaInfo;
import com.ruoyi.jjm.service.IAreaInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地区信息Controller
 * 
 * @author tsr
 * @date 2020-07-30
 */
@Controller
@RequestMapping("/jjm/areaInfo")
public class AreaInfoController extends BaseController
{
    private String prefix = "jjm/areaInfo";

    @Autowired
    private IAreaInfoService areaInfoService;

//    @RequiresPermissions("areaInfo:areaInfo:view")
    @GetMapping()
    public String areaInfo()
    {
        return prefix + "/areaInfo";
    }

    /**
     * 查询地区信息列表
     */
//    @RequiresPermissions("areaInfo:areaInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AreaInfo areaInfo)
    {
        startPage();
        List<AreaInfo> list = areaInfoService.selectAreaInfoList(areaInfo);
        return getDataTable(list);
    }

    /**
     * 导出地区信息列表
     */
//    @RequiresPermissions("areaInfo:areaInfo:export")
    @Log(title = "地区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AreaInfo areaInfo)
    {
        List<AreaInfo> list = areaInfoService.selectAreaInfoList(areaInfo);
        ExcelUtil<AreaInfo> util = new ExcelUtil<AreaInfo>(AreaInfo.class);
        return util.exportExcel(list, "areaInfo");
    }

    /**
     * 新增地区信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存地区信息
     */
//    @RequiresPermissions("areaInfo:areaInfo:add")
    @Log(title = "地区信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AreaInfo areaInfo)
    {
        return toAjax(areaInfoService.insertAreaInfo(areaInfo));
    }

    /**
     * 修改地区信息
     */
    @GetMapping("/edit")
    public String edit(ModelMap mmap)
    {
        String baolianAreaTitle = "包联地区数";
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser.getDeptLevel() == 1){
            baolianAreaTitle = "包联县市（区）数";
        }else if(sysUser.getDeptLevel() == 2){
            baolianAreaTitle = "包联乡镇数";
        }else if(sysUser.getDeptLevel() == 3){
            baolianAreaTitle = "包联村数";
        }else if(sysUser.getDeptLevel() == 4){
            baolianAreaTitle = "包联户数";
        }
        Long areaId = sysUser.getDeptId();
        AreaInfo areaInfo = areaInfoService.selectAreaInfoByAreaId(areaId);
        mmap.put("areaInfo", areaInfo);
        mmap.put("baolianAreaTitle", baolianAreaTitle);
        return prefix + "/edit";
    }

    /**
     * 修改保存地区信息
     */
//    @RequiresPermissions("areaInfo:areaInfo:edit")
    @Log(title = "地区信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AreaInfo areaInfo)
    {
        if(areaInfo.getAreaId() == null){
            Long areaId = ShiroUtils.getSysUser().getDeptId();
            areaInfo.setAreaId(areaId);
        }
        if(areaInfo.getId() == null){
            areaInfo.setCreateBy(ShiroUtils.getLoginName());
            areaInfoService.insertAreaInfo(areaInfo);
        }else{
            areaInfo.setUpdateBy(ShiroUtils.getLoginName());
            areaInfoService.updateAreaInfo(areaInfo);
        }
        return toAjax(1);
    }

    /**
     * 删除地区信息
     */
//    @RequiresPermissions("areaInfo:areaInfo:remove")
    @Log(title = "地区信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(areaInfoService.deleteAreaInfoByIds(ids));
    }
}
